package cn.lijiajia3515.cairo.starter.autoconfigure.security;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.List;

public class CairoSecurityAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean
	CorsConfigurationSource corsConfigurationSource() {
		CorsConfiguration cors = new CorsConfiguration();
		cors.setAllowedOriginPatterns(List.of("http*"));
		// 允许所有公开请求头 跨域
		cors.setExposedHeaders(List.of("*"));
		// 允许 所有请求头跨域
		cors.setAllowedHeaders(List.of("*"));
		// 允许 所有方法跨域
		cors.setAllowedMethods(List.of("*"));
		// 允许携带凭证
		cors.setAllowCredentials(true);
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", cors);
		return source;
	}
}
